package com.bowlong.util;

import java.util.Arrays;

@SuppressWarnings("unchecked")
public class FastArrayList<K> {

	private int space;
	public int size;
	public K[] data;

	public FastArrayList() {
		this(1);
	}

	public FastArrayList(int _space) {
		size = 0;
		space = _space;
		data = (K[]) new Object[space];
	}

	public boolean forwardRefs = false;

	public void add(K o) {
		if (data.length == size) {
			// grow array if necessary
			data = Arrays.copyOf(data, data.length * 2);
		}

		data[size] = o;
		size++;
	}

	public void clear() {
		for (int i = 0; i < size; i++) {
			data[i] = null;
		}
		size = 0;
	}

	public void release() {
		size = 0;
		data = (K[]) new Object[space];
	}

	public int identityIndexOf(Object obj) {
		for (int i = 0; i < size; i++) {
			if (obj == data[i]) {
				forwardRefs = true;
				return i;
			}
		}
		return -1;
	}

	public int capacity() {
		return data.length;
	}

}